package hr.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hr.entity.DO.EmployeeMovementDO;
import hr.entity.EmployeeMovement;
import hr.entity.vo.EmployeeMovementVo;
import hr.mapper.EmployeemovementMapper;
import hr.service.EmployeemovementService;
import hr.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 员工调动记录表 服务实现类
 * </p>
 *
 * @author zzh
 * @since 2023-12-14
 */
@Service
public class EmployeemovementServiceImpl extends ServiceImpl<EmployeemovementMapper, EmployeeMovement> implements EmployeemovementService {
    @Autowired
    private EmployeemovementMapper employeemovementMapper;

    @Override
    public List<EmployeeMovementDO> getEmployeeMovement(EmployeeMovementVo vo) {
        long offset = (vo.getPage() - 1) * vo.getLimit();
        long dateStart=0;
        long dateEnd = 0;
        if (vo.getMovementDate() != null && !vo.getMovementDate().isBlank()) {
            dateStart= CommonUtils.convertYMDDateToTimestamp(vo.getMovementDate());
            dateEnd=dateStart+86400000;
        }
        List<EmployeeMovementDO> employeeMovementDOList = employeemovementMapper.getEmployeeMovement(
                vo.getName(), dateStart,dateEnd, offset, vo.getLimit());
        employeeMovementDOList.forEach(e -> {
            e.setMovementDateStr(CommonUtils.convertTimestampToYMDAndTime(e.getMovementDate()));
        });
        return employeeMovementDOList;
    }

    @Override
    public Integer getCount(EmployeeMovementVo vo) {
        Integer count = employeemovementMapper.getCount(vo.getName(), vo.getMovementDate());
        return count;
    }

    @Override
    public boolean updateDepartmentPostMovement(EmployeeMovementDO movementDO) {
        if (movementDO.getMovementId() != null) {

            String toDepartmentName = movementDO.getToDepartmentName();
            String toPositionName = movementDO.getToPositionName();

            // isBlank 方法用于检查字符串是否为空白，包括 null、空字符串或仅包含空白字符
            boolean isDepartmentUpdate = !toDepartmentName.isBlank();
            boolean isPositionUpdate = !toPositionName.isBlank();

            if (isDepartmentUpdate && isPositionUpdate) {
                throw new IllegalArgumentException("不能同时修改部门和岗位，请分开操作");
            }

            // 优化：三元表达式
            int updateRows = isDepartmentUpdate ?
                    employeemovementMapper.updateDepartment(movementDO.getMovementId(), toDepartmentName) :
                    isPositionUpdate ?
                            employeemovementMapper.updatePosition(movementDO.getMovementId(), toPositionName) :
                            0; // 如果没有任何更新操作，设置为 0

            return updateRows > 0; // 返回更新是否成功

//            // 只更新部门名称
//            if (isDepartmentUpdate) {
//                Integer updateRows = employeemovementMapper.updateDepartment(movementDO.getMovementId(), toDepartmentName);
//                return updateRows > 0;
//            }
//            // 只更新岗位名称
//            else if (isPositionUpdate) {
//                Integer updateRows = employeemovementMapper.updatePosition(movementDO.getMovementId(), toPositionName);
//                return updateRows > 0;
//            }
        }
        return false;
    }

    @Override
    public List<EmployeeMovementDO> getEmployeeMovementByEmpId(Long employeeId) {
        return employeemovementMapper.selectEmployeeMovementByEmpId(employeeId);
    }
}
